package com.spring.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.spring.gowhere.R;
import com.spring.model.ImageData;
import com.spring.model.ReturnImageList;
import com.spring.util.ImageBetter;

public class ImageAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	AQuery aQuery;
	private static final int[] ids = { R.drawable.ad1,R.drawable.ad2, R.drawable.ad3,
	 R.drawable.ad4,R.drawable.ad5 };
	//private String[] ids;
	public ReturnImageList imagelist;

	public ImageAdapter(Context context) {
		mContext = context;
		aQuery = new AQuery(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public ImageAdapter(Context context, String[] ids, ReturnImageList imagelist) {
		mContext = context;
		aQuery = new AQuery(context);
		//this.ids = ids;
		this.imagelist = imagelist;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE; // 返回最大值来实现循环
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.image_item, null);
			holder.mImage = (ImageView) convertView.findViewById(R.id.imgView);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}
		holder.mImage.setImageResource(ids[position % ids.length]);
		//ImageBetter.getInstance().displayImage(ids[position % ids.length],holder.mImage);
//		aQuery.id(holder.mImage).image(
//				ids[position % ids.length]);

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				if (imagelist.ImageList != null
//						&& imagelist.ImageList.size() > 0) {
//					String linkUrl = imagelist.ImageList.get(position % ids.length + 1).LinkUrl;
//					Log.e("sdf", "xq---"+linkUrl);
//					Intent intent = new Intent();
//					intent.setAction("android.intent.action.VIEW");
//					Uri content_url = Uri.parse(linkUrl);
//					intent.setData(content_url);
//					mContext.startActivity(intent);
//				}

				String linkUrl = "http://www.yczn.net.cn";
				Intent intent = new Intent();
				intent.setAction("android.intent.action.VIEW");
				Uri content_url = Uri.parse(linkUrl);
				intent.setData(content_url);
				mContext.startActivity(intent);

				// Toast.makeText(
				// mContext,
				// "点击了第" + (position % imagelist.ImageList.size() + 1)
				// + "张图片", Toast.LENGTH_SHORT).show();

			}
		});
		return convertView;
	}

	public class ViewHolder {
		public ImageView mImage;
	}

}
